/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */

package l1j.jrwz.server.clientpackets;

import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import l1j.jrwz.server.ActionCodes;
import l1j.jrwz.server.ClientThread;
import l1j.jrwz.server.model.Instance.L1ItemInstance;
import l1j.jrwz.server.model.Instance.L1PcInstance;
import l1j.jrwz.server.model.Instance.L1PetInstance;
import l1j.jrwz.server.serverpackets.S_DoActionGFX;
import l1j.jrwz.server.serverpackets.S_DoActionShop;
import l1j.jrwz.server.serverpackets.S_ServerMessage;
import l1j.jrwz.server.templates.L1PrivateShopBuyList;
import l1j.jrwz.server.templates.L1PrivateShopSellList;

// Referenced classes of package l1j.jrwz.server.clientpackets:
// ClientBasePacket

/**
 * 處理收到由客戶端傳來商店的封包
 */
public class C_Shop extends ClientBasePacket {

    private static final String C_SHOP = "[C] C_Shop";

    private static Logger _log = Logger.getLogger(C_Shop.class.getName());

    @Override
    public void execute(byte[] decrypt, ClientThread client) {
        try {
            read(decrypt);

            L1PcInstance pc = client.getActiveChar();
            if (pc == null) {
                return;
            }
            if (pc.isDead()) {
                return;
            }
            if (pc.isGhost()) {
                return;
            }

            int mapId = pc.getMapId();
            if (mapId != 340 && mapId != 350 && mapId != 360 && mapId != 370) {
                pc.sendPackets(new S_ServerMessage(876)); // 无法在此开设个人商店。
                return;
            }

            ArrayList<L1PrivateShopSellList> sellList = pc.getSellList();
            ArrayList<L1PrivateShopBuyList> buyList = pc.getBuyList();
            L1ItemInstance checkItem;
            boolean tradable = true;

            int type = readC();
            if (type == 0) { // 開始
                int sellTotalCount = readH();
                int sellObjectId;
                int sellPrice;
                int sellCount;
                for (int i = 0; i < sellTotalCount; i++) {
                    sellObjectId = readD();
                    sellPrice = readD();
                    sellCount = readD();
                    // 檢查交易項目
                    checkItem = pc.getInventory().getItem(sellObjectId);
                    if (!checkItem.getItem().isTradable()) {
                        tradable = false;
                        pc.sendPackets(new S_ServerMessage(166, // \f1%0%s %4%1%3 %2.
                                checkItem.getItem().getName(), "这是不可能处理。"));
                    }
                    Object[] petlist = pc.getPetList().values().toArray();
                    for (Object petObject : petlist) {
                        if (petObject instanceof L1PetInstance) {
                            L1PetInstance pet = (L1PetInstance) petObject;
                            if (checkItem.getId() == pet.getItemObjId()) {
                                tradable = false;
                                pc.sendPackets(new S_ServerMessage(
                                        166, // \f1%0%s %4%1%3 %2.
                                        checkItem.getItem().getName(),
                                        "这是不可能处理。"));
                                break;
                            }
                        }
                    }
                    L1PrivateShopSellList pssl = new L1PrivateShopSellList();
                    pssl.setItemObjectId(sellObjectId);
                    pssl.setSellPrice(sellPrice);
                    pssl.setSellTotalCount(sellCount);
                    sellList.add(pssl);
                }
                int buyTotalCount = readH();
                int buyObjectId;
                int buyPrice;
                int buyCount;
                for (int i = 0; i < buyTotalCount; i++) {
                    buyObjectId = readD();
                    buyPrice = readD();
                    buyCount = readD();
                    // 檢查交易項目
                    checkItem = pc.getInventory().getItem(buyObjectId);
                    if (!checkItem.getItem().isTradable()) {
                        tradable = false;
                        pc.sendPackets(new S_ServerMessage(166, // \f1%0%s %4%1%3 %2.
                                checkItem.getItem().getName(), "这是不可能处理。"));
                    }
                    if (checkItem.getBless() >= 128) { // 封印的裝備
                        // \f1%0%d 是不可丢弃或转移的
                        pc.sendPackets(new S_ServerMessage(210, checkItem
                                .getItem().getName()));
                        return;
                    }

                    Object[] petlist = pc.getPetList().values().toArray();
                    for (Object petObject : petlist) {
                        if (petObject instanceof L1PetInstance) {
                            L1PetInstance pet = (L1PetInstance) petObject;
                            if (checkItem.getId() == pet.getItemObjId()) {
                                tradable = false;
                                pc.sendPackets(new S_ServerMessage(
                                        166, // \f1%0%s %4%1%3 %2.
                                        checkItem.getItem().getName(),
                                        "这是不可能处理。"));
                                break;
                            }
                        }
                    }
                    L1PrivateShopBuyList psbl = new L1PrivateShopBuyList();
                    psbl.setItemObjectId(buyObjectId);
                    psbl.setBuyPrice(buyPrice);
                    psbl.setBuyTotalCount(buyCount);
                    buyList.add(psbl);
                }
                if (!tradable) { // 如果項目不包括在交易結束零售商
                    sellList.clear();
                    buyList.clear();
                    pc.setPrivateShop(false);
                    pc.sendPackets(new S_DoActionGFX(pc.getId(),
                            ActionCodes.ACTION_Idle));
                    pc.broadcastPacket(new S_DoActionGFX(pc.getId(),
                            ActionCodes.ACTION_Idle));
                    return;
                }
                byte[] chat = readByte();
                pc.setShopChat(chat);
                pc.setPrivateShop(true);
                pc.sendPackets(new S_DoActionShop(pc.getId(),
                        ActionCodes.ACTION_Shop, chat));
                pc.broadcastPacket(new S_DoActionShop(pc.getId(),
                        ActionCodes.ACTION_Shop, chat));
            } else if (type == 1) { // 終了
                sellList.clear();
                buyList.clear();
                pc.setPrivateShop(false);
                pc.sendPackets(new S_DoActionGFX(pc.getId(),
                        ActionCodes.ACTION_Idle));
                pc.broadcastPacket(new S_DoActionGFX(pc.getId(),
                        ActionCodes.ACTION_Idle));
            }
        } catch (final Exception e) {
            _log.log(Level.SEVERE, e.getLocalizedMessage(), e);
        } finally {
            finish();
        }
    }

    @Override
    public String getType() {
        return C_SHOP;
    }

}
